package tablet.client.workspace;

import java.io.IOException;

import websocket4j.client.WebSocket;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

public class CommunicationThread extends AsyncTask<String, Void, Void>{

	WebSocket ws = null;
	CommunicationActivity commA = null;
	String tv_information;
	boolean steelAlive;
	TextView infoTv;
	
	public CommunicationThread(CommunicationActivity commA, TextView infoTv){
		this.commA = commA;
		this.steelAlive = false;
		this.infoTv = infoTv;
	}
	
	@Override
	protected Void doInBackground(String... info) {
		
			try {
	//			Log.i("connecting...", "pased");
				ws = new WebSocket(info[0], Integer.valueOf(info[1]), "");
				try {
					ws.sendMessage("GET");
					tv_information = ws.getMessage();
					if(tv_information != null){
						steelAlive = true;
					}
				} catch (IOException e) {
					e.printStackTrace();
				}

//				while(true){
//					if(steelAlive){
//						tv_information 	= ws.getMessage();
//					}else{
//						tv_information = "Server closed...";
//						ws.close();
//						break;
//					}
				
		        	infoTv.setText(tv_information);
//				}
//				return tv_information;
//				return ws;
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		
	}
	
	
	public String getTvInformation(){
		return this.tv_information;
	}
	
	public String askData(String tData){
		String message = null;
		try {
			ws.sendMessage(tData);
			message = ws.getMessage();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return message;
	}
	

	
//	@Override
//	protected void onProgressUpdate(Void... values){
//		try {
//			while(true){
//				if(steelAlive){
//					tv_information 	= ws.getMessage();
//				}else{
//					tv_information = "Server closed...";
//					ws.close();
//					break;
//				}
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
	
//	@Override
	protected void onPostExecute(Void result){
		super.onPostExecute(result);
		infoTv.setText(tv_information);
//		try {
//			while(true){
//				if(steelAlive){
//					tv_information 	= ws.getMessage();
//				}else{
//					tv_information = "Server closed...";
//					ws.close();
//					break;
//				}
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		
	}


	
}
